/**
 * Makes board for 2040Circle
 * 
 * @author Kendall Zhu Hi Kendall! 
 * @version 2014
 */
public class Board
{
    private int[][] board;
/**
Creates new 2D array board w/ 4 rows and 8 cols
Sets each value in board array to 0
*/
    public Board()
    {
        board=new int[4][8];
        for(int row=0; row<4; row++)
        {
            for(int col=0; col<8; col++)
            {
                set(row,col,0);
            }
        }
    }
/**
  Returns the value at a given location in board
  @return  The value in board at:
  @param row  The row in board 
  @param col  The column in board 
*/  
    public int get(int row, int col)
    {
        return board[row][col];
    }
/**
  Sets the value in board at the given location to: 
  @param value  The value the location will be set to
  @param row  The row in board
  @param col  The column in board
*/
    public void set(int row, int col, int value)
    {
        board[row][col]=value;
    }
/**
  Rotates the values of a specific row of board one to the right 
  @param row  The row in board to be rotated right
*/
    public void rotateRight(int row)
    {
        int last=get(row,3);
        for(int i=7; i>0; i--)
        {
            set(row,i,get(row,i-1));
        }
        set(row, 0, last);
    }
/**
  Rotates the values of a specific row of board one to the left
  @param row The row in board to be rotated left
*/
    public void rotateLeft(int row)
    {
        int first=get(row,0);
        for(int i=0; i<8; i++)
        {
            set(row,i,get(row,i+1));
        }
        set(row, 3, first);
    }
/**
Checks if any values in the columns of board can be combined if the user 
tries to combine inwards
@return  whether or not any values can be combined (true or false)
*/
    public boolean combineIn()
    {
        boolean ret=false;
        for(int col=0; col<4; col++)
        {
            for(int i=0; i<10; i++)
            {
                for(int row=0; row<3; row++)
                {
                    if(get(row,col)==0)
                    {
                        set(row,col,get(row,col)+get(row+1,col));
                        set(row+1,col,0);
                        ret=true;
                    }
                    //trying to prevent double combining
                    if(get(row,col)==get(row+1,col))
                    {
                        set(row,col,get(row,col)+get(row+1,col));
                        set(row+1,col,0);
                        ret=true;
                    }
                }
            }
        }
        return ret;    
    }
    
 /**
Checks if any values in the columns of board can be combined if the user 
tries to combine outwards
@return  whether or not any values can be combined (true or false)
*/
    public boolean combineOut()
    {
        boolean ret=true;
        for(int col=0; col<4; col++)
        {
            for(int i=0; i<10; i++)
            {
                for(int row=3; row>0; row--)
                {
                    if(get(row,col)==0 || get(row,col)==get(row-1,col))
                    {
                        set(row,col,get(row,col)+get(row-1,col));
                        set(row+1,col,0);
                        ret=true;
                    }
                }
            }
        }
        return ret;
    }
/**
Generates a random row and column (column is the remainder
when row is divded by 4)
Adds a 2 in that location of board
*/
    public void spawn2()
    {
        int space=(int)(Math.random()*32);
        int row=space/8;
        int col=space%4;
        while(get(row,col)!=0)
        {
            space=(int)(Math.random()*32);
            row=space/8;
            col=space%4;
        }
        set(row,col,2);
    }
/**
Generates a random row and column (column is the remainder
when row is divded by 4)
Adds a 4 in that location of board
*/
    public void spawn4()
    {
        int space=(int)(Math.random()*32);
        int row=space/8;
        int col=space%4;
        while(get(row,col)!=0)
        {
            space=(int)(Math.random()*32);
            row=space/8;
            col=space%4;
        }
        set(row,col,4);
    }
    public boolean notMerged(int col)
    {
        return true;//STUB
        //checks if there are spaces or duplicates in a column currently not in use; instead merge just goes through 10 times because I know it shouldn't take more than that
    }
/**
Prints the values in board as a 2D array
*/
    public void print()
    {
        for(int i=0; i<4; i++)
        {
            for(int j=0; j<8; j++)
            {
                System.out.print(get(i,j)+" ");
            }
            System.out.println();
        }
        System.out.println();
    }
}
